package net.jlibrary.common.project.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;

import com.fasterxml.jackson.annotation.JsonFormat;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.SearchCondition;
import net.jlibrary.common.core.annotation.SearchField;
import net.jlibrary.common.core.entity.BaseObject;

/**
 * 操作日志:操作实体对象时，改变或增加的列字段的变动
 * 
 * @author sunlf
 *
 */
@Entity(name = "tab_operationfield")
public class OperationField extends BaseObject {
	private static final long serialVersionUID = -8863132077134483858L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "log_id")
	@SearchField(value = "operationLog", condition = SearchCondition.EQUAL)
	private OperationLog operationLog;
	@Column(length = 50)
	private String fieldName;// 列名
	@Column(length = 50)
	private String fieldDesc;// 列描述，如果jpa有描述的值，取描述的值，如果没有则为空
	@Column(length = 500)
	private String oldValue;// 旧值，如果是新增，则为空
	@Column(length = 500)
	private String newValue;// 改变后的值，如果是新增，填入这里
	@Column(updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT)
	@JsonFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT, timezone = CoreConstants.DATE_TIMEZONE_DEFAULT)
	private Date createdate;
	@Temporal(TemporalType.TIMESTAMP)
	@DateTimeFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT)
	@JsonFormat(pattern = CoreConstants.DATETIME_FORMAT_DEFAULT, timezone = CoreConstants.DATE_TIMEZONE_DEFAULT)
	private Date lastdate;

	public Long getId() {
		return id;
	}
	@Override
	@Transient
	public Serializable getPrimaryKey() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public OperationLog getOperationLog() {
		return operationLog;
	}

	public void setOperationLog(OperationLog operationLog) {
		this.operationLog = operationLog;
	}

	public String getFieldName() {
		return fieldName;
	}

	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	public String getFieldDesc() {
		return fieldDesc;
	}

	public void setFieldDesc(String fieldDesc) {
		this.fieldDesc = fieldDesc;
	}

	public String getOldValue() {
		return oldValue;
	}

	public void setOldValue(String oldValue) {
		this.oldValue = oldValue;
	}

	public String getNewValue() {
		return newValue;
	}

	public void setNewValue(String newValue) {
		this.newValue = newValue;
	}
	
	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Date getLastdate() {
		return lastdate;
	}

	public void setLastdate(Date lastdate) {
		this.lastdate = lastdate;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((fieldDesc == null) ? 0 : fieldDesc.hashCode());
		result = prime * result + ((fieldName == null) ? 0 : fieldName.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((newValue == null) ? 0 : newValue.hashCode());
		result = prime * result + ((oldValue == null) ? 0 : oldValue.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OperationField other = (OperationField) obj;
		if (fieldDesc == null) {
			if (other.fieldDesc != null)
				return false;
		} else if (!fieldDesc.equals(other.fieldDesc))
			return false;
		if (fieldName == null) {
			if (other.fieldName != null)
				return false;
		} else if (!fieldName.equals(other.fieldName))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (newValue == null) {
			if (other.newValue != null)
				return false;
		} else if (!newValue.equals(other.newValue))
			return false;
		if (oldValue == null) {
			if (other.oldValue != null)
				return false;
		} else if (!oldValue.equals(other.oldValue))
			return false;
		return true;
	}
}
